#include "json/cJSON.h"
#include <stdlib.h>
#include "log/log.h"

int main(int argc, char const *argv[]) {
 
    char *name="xiaosongli";
    int age=18;
    cJSON *root=cJSON_CreateObject();
    cJSON_AddStringToObject(root,"name",name);
    cJSON_AddNumberToObject(root,"age",age);
    //将对象转化为未格式化的字符串
    char *out=cJSON_PrintUnformatted(root);
    log_info("out=%s",out);

    //解析字符串
    //解析字符串得到的json对象
    cJSON *json=cJSON_Parse(out);
    //获取属性值
    cJSON *json_name=cJSON_GetObjectItem(json,"name");
    cJSON *json_age=cJSON_GetObjectItem(json,"age");
    if (json_name==NULL || json_age==NULL)
    {
        log_error("json中没有数据!");
        return -1;
    }
    
    log_info("解析json:name=%s,age=%d",json_name->valuestring,json_age->valueint);
    //释放内存
    cJSON_Delete(json);
    free(out);
    cJSON_Delete(root);
    return 0;
}